package com.mls.guardian.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * Created by root on 15-12-10.
 */
public class VersionConfig {
    private static final String basePath = System.getProperty("user.home") + File.separator + "guardianConfig";

    /**
     * 写入数据
     *
     * @param key
     * @param value
     */
    public static void write(String key, String value) {
        Properties prop = new Properties();

        FileOutputStream fos = null;
        try {
            prop.setProperty(key, value);
            fos = new FileOutputStream(Paths.get(basePath, "version.properties").toString());
            prop.store(fos, String.valueOf(System.currentTimeMillis()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static String getVersion() {
        Properties prop = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(Paths.get(basePath, "version.properties").toString());
            prop.load(fis);

            return prop.getProperty("upgradeVersion", "");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return "";
    }
}
